package com.qf.service.impl;

import com.qf.service.GoodsService;
import com.qf.dao.CartDao;
import com.qf.dao.GoodsDao;
import com.qf.dao.GoodsTypeDao;
import com.qf.entity.PageBean;
import com.qf.pojo.Goods;
import com.qf.pojo.GoodsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    CartDao cartDao;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    GoodsTypeDao dao;

    @Override
    public PageBean<Goods> findGoodsListByTypeIdPage(int typeId, int pageSize, int pageNum) {
        PageBean<Goods> pageBean = new PageBean<>();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        pageBean.setStartPage(1);

        //?????
        long count = goodsDao.getTotalCount(typeId);
        pageBean.setTotalCount(count);

        //?????
        int startIndex = pageSize * (pageNum - 1);
        List<Goods> data = goodsDao.getData(typeId, startIndex, pageSize);
        pageBean.setData(data);

        return pageBean;
    }

    @Override
    public Goods findById(int id) {
        //查询的商品详细信息
        Goods goods = goodsDao.findById(id);

        //获取商品分类id
        int typeid = goods.getTypeId();

        //查询商品分类信息
        GoodsType byId = dao.findById(id);
        goods.setGoodsType(byId);

        return goods;
    }
}
